﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using BCCommon;
using BCEntity.Common.WebsiteConfiguration;
using Dapper;

namespace BCData.Common.WebsiteConfiguration
{
    /// <summary>
    /// 系统配置信息数据处理
    /// </summary>
    public class WebsiteConfigurationData : IWebsiteConfigurationData
    {
        private readonly IDatabaseContext databaseContext;
        private const string QUERY = @"SELECT WebsiteConfigurationId,WebsiteName,Logo,QuadrateLogo,Host,WebsiteKeyword,Description,
                                              BackgroundImage,ServiceTelephone,WeChatQRUrl,WeChatQRName,CompanyName,CompanyAddress,
                                              CompanyCoordinate,CompanyZIP,CompanyEmail,Superintendent,SuperintendentPhone,WorkingTime,ServiceTelephone,
                                              ICPNo,PublicSecurityNo
                                        FROM WebsiteConfiguration";
        public WebsiteConfigurationData(IDatabaseContext databaseContext)
        {
            this.databaseContext = databaseContext;
        }

        public WebsiteConfigurationEntity Get()
        {
            return databaseContext.Slave.QueryFirstOrDefault<WebsiteConfigurationEntity>(string.Format("{0} LIMIT 1;", QUERY));
        }

        public WebsiteConfigurationEntity Get(int websiteConfigurationId)
        {
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WebsiteConfigurationId", websiteConfigurationId, DbType.Int32, size: 10);
            return databaseContext.Slave.QueryFirstOrDefault<WebsiteConfigurationEntity>(string.Format("{0} WHERE WebsiteConfigurationId=?WebsiteConfigurationId;", QUERY), parameters);
        }

        public WebsiteConfigurationEntity Update(WebsiteConfigurationEntity websiteConfigurationEntity)
        {
            string sql = @"UPDATE WebsiteConfiguration SET
                                WebsiteName=?WebsiteName,
                                Logo=?Logo,
                                QuadrateLogo=?QuadrateLogo,
                                Host=?Host,
                                WebsiteKeyword=?WebsiteKeyword,
                                Description=?Description,
                                BackgroundImage=?BackgroundImage,
                                ServiceTelephone=?ServiceTelephone,
                                WeChatQRUrl=?WeChatQRUrl,
                                WeChatQRName=?WeChatQRName,
                                CompanyName=?CompanyName,
                                CompanyAddress=?CompanyAddress,
                                CompanyCoordinate=?CompanyCoordinate,
                                CompanyZIP=?CompanyZIP,
                                CompanyEmail=?CompanyEmail,
                                Superintendent=?Superintendent,
                                SuperintendentPhone=?SuperintendentPhone,
                                WorkingTime=?WorkingTime,
                                ICPNo=?ICPNo,
                                PublicSecurityNo=?PublicSecurityNo
                           WHERE WebsiteConfigurationId=?WebsiteConfigurationId;";
            sql = string.Concat(sql,string.Format("{0} WHERE WebsiteConfigurationId=?WebsiteConfigurationId;", QUERY));
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WebsiteConfigurationId", websiteConfigurationEntity.WebsiteConfigurationId, DbType.Int32, size: 10);
            parameters.Add("?WebsiteName", websiteConfigurationEntity.WebsiteName, DbType.AnsiString, size: 100);
            parameters.Add("?Logo", websiteConfigurationEntity.Logo, DbType.AnsiString, size: 1000);
            parameters.Add("?QuadrateLogo", websiteConfigurationEntity.QuadrateLogo, DbType.AnsiString, size: 1000);
            parameters.Add("?Host", websiteConfigurationEntity.Host, DbType.AnsiString, size: 100);
            parameters.Add("?WebsiteKeyword", websiteConfigurationEntity.WebsiteKeyword, DbType.AnsiString, size: 1000);
            parameters.Add("?Description", websiteConfigurationEntity.Description, DbType.AnsiString, size: 1000);
            parameters.Add("?BackgroundImage", websiteConfigurationEntity.BackgroundImage, DbType.AnsiString, size: 1000);
            parameters.Add("?ServiceTelephone", websiteConfigurationEntity.ServiceTelephone, DbType.AnsiString, size: 20);
            parameters.Add("?WeChatQRUrl", websiteConfigurationEntity.WeChatQRUrl, DbType.AnsiString, size: 1000);
            parameters.Add("?WeChatQRName", websiteConfigurationEntity.WeChatQRName, DbType.AnsiString, size: 50);
            parameters.Add("?CompanyName", websiteConfigurationEntity.CompanyName, DbType.AnsiString, size: 100);
            parameters.Add("?CompanyAddress", websiteConfigurationEntity.CompanyAddress, DbType.AnsiString, size: 100);
            parameters.Add("?CompanyCoordinate", websiteConfigurationEntity.CompanyCoordinate, DbType.AnsiString, size: 50);
            parameters.Add("?CompanyZIP", websiteConfigurationEntity.CompanyZIP, DbType.AnsiString, size: 20);
            parameters.Add("?CompanyEmail", websiteConfigurationEntity.CompanyEmail, DbType.AnsiString, size: 20);
            parameters.Add("?Superintendent", websiteConfigurationEntity.Superintendent, DbType.AnsiString, size: 20);
            parameters.Add("?SuperintendentPhone", websiteConfigurationEntity.SuperintendentPhone, DbType.AnsiString, size: 20);
            parameters.Add("?WorkingTime", websiteConfigurationEntity.WorkingTime, DbType.AnsiString, size: 50);
            parameters.Add("?ICPNo", websiteConfigurationEntity.ICPNo, DbType.AnsiString, size: 50);
            parameters.Add("?PublicSecurityNo", websiteConfigurationEntity.PublicSecurityNo, DbType.AnsiString, size: 50);
            return this.databaseContext.Master.QueryFirstOrDefault<WebsiteConfigurationEntity>(sql, parameters, databaseContext.Transaction);
        }

    }
}
